import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'
declare let AMap:any;
@Component({
  selector: 'app-device-gis',
  templateUrl: './device-gis.component.html',
  styleUrls: ['./device-gis.component.css']
})
export class DeviceGisComponent implements OnInit {

  map:any;

  constructor( private activeRoute:ActivatedRoute) { }

  ngOnInit() {


    this.activeRoute.params.subscribe( params => {
      this.map = new AMap.Map("container", {
        resizeEnable: true
      });
      this.geocoder(params['addr'])
    });
  }

  geocoder(addr){
    let geocoder = new AMap.Geocoder({
      radius: 500
    });

    geocoder.getLocation(addr, (status, result) => {
      if (status === 'complete' && result.info === 'OK') {
        this.geocoder_CallBack(result);
      }
    });
  }

  addMarker(i, d) {
    let marker = new AMap.Marker({
      map: this.map,
      position: [ d.location.getLng(),  d.location.getLat()]
    });
    let infoWindow = new AMap.InfoWindow({
      content: d.formattedAddress,
      offset: {x: 0, y: -30}
    });
    marker.on("mouseover", (e) => {
      infoWindow.open(this.map, marker.getPosition());
    });
  }

  geocoder_CallBack(data) {
    let geocode = data.geocodes;
    for (let i = 0; i < geocode.length; i++) {
      this.addMarker(i, geocode[i]);
    }
    this.map.setFitView();
  }
}
